<html>
<head><meta charset="utf-8"><title>String Interpolation · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html">String Interpolation</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="230713638"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230713638" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeff <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230713638">(Mar 17 2021 at 15:51)</a>:</h4>
<p>So I've been slowly learning rust, and so far, I have been loving it. One question I have though, is why not have string interpolation? </p>
<div class="codehilite"><pre><span></span><code>
</code></pre></div>



<a name="230713851"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230713851" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeff <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230713851">(Mar 17 2021 at 15:52)</a>:</h4>
<p>Oops, hit enter too fast XD<br>
Anyways,</p>
<div class="codehilite"><pre><span></span><code>fn main() {
   let x: usize = 100000000;

    println!(&quot;{}&quot;, x);
}
</code></pre></div>
<p>Here, </p>
<div class="codehilite"><pre><span></span><code>
</code></pre></div>



<a name="230714016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230714016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeff <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230714016">(Mar 17 2021 at 15:52)</a>:</h4>
<p>God dang it, not again XD<br>
For the third time</p>
<div class="codehilite"><pre><span></span><code>println!(&quot;{}&quot;,  x);
</code></pre></div>



<a name="230714313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230714313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230714313">(Mar 17 2021 at 15:54)</a>:</h4>
<p>you might find this helpful: <a href="https://blog.thoughtram.io/string-vs-str-in-rust/">https://blog.thoughtram.io/string-vs-str-in-rust/</a></p>



<a name="230714610"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230714610" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeff <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230714610">(Mar 17 2021 at 15:55)</a>:</h4>
<p>Im just going to give up now.<br>
Why not incorporate something like </p>
<div class="codehilite"><pre><span></span><code>println!(&quot;{x}&quot;);
</code></pre></div>
<p>?<br>
This is, imo, much easier to read</p>



<a name="230714739"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230714739" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230714739">(Mar 17 2021 at 15:56)</a>:</h4>
<p>that is not valid syntax the interior of the braces is reserved for formatting specifiers</p>



<a name="230714854"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230714854" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230714854">(Mar 17 2021 at 15:57)</a>:</h4>
<p>what you have suggested is not conventional and would be confusing to many readers</p>



<a name="230716029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230716029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230716029">(Mar 17 2021 at 16:02)</a>:</h4>
<p>it was recently suggested that <code>println!(x)</code> could automatically interpretate <code>x</code> without the <code>{}</code></p>



<a name="230716112"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230716112" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230716112">(Mar 17 2021 at 16:02)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281739">oliver</span> <a href="#narrow/stream/213817-t-lang/topic/String.20Interpolation/near/230714854">said</a>:</p>
<blockquote>
<p>what you have suggested is not conventional and would be confusing to many readers</p>
</blockquote>
<p>well... it's very common in python, and I think lots of ppl like that syntax. But it's also very hard to get this right, as you have to parse arbitrary expressions within a string. This may require something similar to python where you prefix the formatting string with a letter (example: <code>f"{x}"</code>)</p>



<a name="230716474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230716474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230716474">(Mar 17 2021 at 16:03)</a>:</h4>
<p>I think if we just tried to do that for <code>format_args!</code> and friends, we may run into hygiene and parser troubles that aren't easy to solve and may even be backwards incompatible.</p>



<a name="230716555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230716555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230716555">(Mar 17 2021 at 16:03)</a>:</h4>
<p>()</p>



<a name="230716646"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230716646" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230716646">(Mar 17 2021 at 16:03)</a>:</h4>
<p>it's not around variables, you can put arbitrary expressions in there</p>



<a name="230716875"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230716875" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230716875">(Mar 17 2021 at 16:04)</a>:</h4>
<p>I believe there is a plan to make <code>println!("{x}");</code> work.</p>



<a name="230716978"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230716978" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230716978">(Mar 17 2021 at 16:04)</a>:</h4>
<p>sure in Julia you can do "1 + 1 = $(1+ 1)" which is reminiscent of Bash</p>



<a name="230716989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230716989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230716989">(Mar 17 2021 at 16:04)</a>:</h4>
<p><a href="https://rust-lang.github.io/rfcs/2795-format-args-implicit-identifiers.html">https://rust-lang.github.io/rfcs/2795-format-args-implicit-identifiers.html</a></p>



<a name="230719401"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230719401" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230719401">(Mar 17 2021 at 16:13)</a>:</h4>
<p>yes, it's actually implemented and works today, though it is not yet stable:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(format_args_capture)]</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">42</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{x}"</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>IIRC it is intended to be stabilized after the 2021 edition comes out, due to some historical peculiarities with the <code>panic!</code> macro that need an edition change to address</p>



<a name="230721340"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230721340" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230721340">(Mar 17 2021 at 16:21)</a>:</h4>
<p>()</p>



<a name="230721545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230721545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230721545">(Mar 17 2021 at 16:22)</a>:</h4>
<p>well, if your use case is doing println debugging, that's what the <code>dbg!(x)</code> macro is for</p>



<a name="230736367"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230736367" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230736367">(Mar 17 2021 at 17:48)</a>:</h4>
<p><span class="user-mention silent" data-user-id="398492">Jeff</span> <a href="#narrow/stream/213817-t-lang/topic/String.20Interpolation/near/230714610">said</a>:</p>
<blockquote>
<p>Im just going to give up now.<br>
Why not incorporate something like </p>
<div class="codehilite"><pre><span></span><code>println!(&quot;{x}&quot;);
</code></pre></div>
<p>?<br>
This is, imo, much easier to read</p>
</blockquote>
<p>yes, and both the lang and libs team <a href="https://github.com/rust-lang/rfcs/pull/2795#issuecomment-549213465">agreed</a> with that! <a href="https://rust-lang.github.io/rfcs/2795-format-args-implicit-identifiers.html">rfc 2795</a> proposed exactly that, and was accepted :)</p>
<p>it's already implemented, but not yet available in stable rust. there's still some small unfinished parts though, if i understand correctly. (e.g. the behaviour around concat!() and some other things, maybe.)</p>
<p>one of the blockers was that <code>panic!("{x}")</code> was 'accidentally' already accepted and would be confusing when it didn't work with this feature. this is fixed in edition 2021. however, in all editions we now warn about that case anyway, so we can probably just enable this feature in all editions. <a href="/user_uploads/4715/xYz3Oij8ADHwhK-QqvuYu4VM/image.png">image.png</a></p>
<div class="message_inline_image"><a href="/user_uploads/4715/xYz3Oij8ADHwhK-QqvuYu4VM/image.png" title="image.png"><img src="/user_uploads/4715/xYz3Oij8ADHwhK-QqvuYu4VM/image.png"></a></div>



<a name="230755270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/String%20Interpolation/near/230755270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/String.20Interpolation.html#230755270">(Mar 17 2021 at 19:49)</a>:</h4>
<p>And in the meantime, there is <a href="https://docs.rs/fstrings">https://docs.rs/fstrings</a> <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span> :</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">println_f</span><span class="o">!</span><span class="p">(</span><span class="s">"{x}"</span><span class="p">);</span><span class="w"></span>
</code></pre></div>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>